1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.base.Preconditions;
23
24 import java.util.List;
25
26 import javax.annotation.Nullable;
27
28
29
30
31
32
33 @GwtCompatible(serializable = true, emulated = true)
34 @SuppressWarnings("serial")
35 final class SingletonImmutableList<E> extends ImmutableList<E> {
36
37 final transient E element;
38
39 SingletonImmutableList(E element) {
40 this.element = checkNotNull(element);
41 }
42
43 @Override
44 public E get(int index) {
45 Preconditions.checkElementIndex(index, 1);
46 return element;
47 }
48
49 @Override public int indexOf(@Nullable Object object) {
50 return element.equals(object) ? 0 : -1;
51 }
52
53 @Override public UnmodifiableIterator<E> iterator() {
54 return Iterators.singletonIterator(element);
55 }
56
57 @Override public int lastIndexOf(@Nullable Object object) {
58 return indexOf(object);
59 }
60
61 @Override
62 public int size() {
63 return 1;
64 }
65
66 @Override public ImmutableList<E> subList(int fromIndex, int toIndex) {
67 Preconditions.checkPositionIndexes(fromIndex, toIndex, 1);
68 return (fromIndex == toIndex) ? ImmutableList.<E>of() : this;
69 }
70
71 @Override public ImmutableList<E> reverse() {
72 return this;
73 }
74
75 @Override public boolean contains(@Nullable Object object) {
76 return element.equals(object);
77 }
78
79 @Override public boolean equals(@Nullable Object object) {
80 if (object == this) {
81 return true;
82 }
83 if (object instanceof List) {
84 List<?> that = (List<?>) object;
85 return that.size() == 1 && element.equals(that.get(0));
86 }
87 return false;
88 }
89
90 @Override public int hashCode() {
91
92
93 return 31 + element.hashCode();
94 }
95
96 @Override public String toString() {
97 String elementToString = element.toString();
98 return new StringBuilder(elementToString.length() + 2)
99 .append('[')
100 .append(elementToString)
101 .append(']')
102 .toString();
103 }
104
105 @Override public boolean isEmpty() {
106 return false;
107 }
108
109 @Override boolean isPartialView() {
110 return false;
111 }
112
113 @Override
114 int copyIntoArray(Object[] dst, int offset) {
115 dst[offset] = element;
116 return offset + 1;
117 }
118 }